/**
 * @version 1.8
 * @Author Djx_hmbb
 * @Date 2023/8/11 21:56
 * @注释:感受单线程和多线程的时间差
 */
public class ThreadDemo11 {
    public static void main(String[] args) {
//        现在有两个变量,都要自增到10000_0000
//        可以同时自增
//        也可以a先自增,b再自增
        fun2();
    }

    //单线程
    public static void fun1(){
        long beg = System.currentTimeMillis();
        long a = 0;
        for (int i = 0; i < 10000_0000; i++) {
            ++a;
        }

        long b = 0;
        for (int i = 0; i < 10000_0000; i++) {
            ++b;
        }

        long end = System.currentTimeMillis();
        System.out.println("fun1()的时间差为:" + (end-beg) + "ms");
    }

    //多线程
    public static void fun2(){
        Thread t1 = new Thread(()->{
            long a = 0;
            for (int i = 0; i < 10000_0000; i++) {
                ++a;
            }
            });

        Thread t2 = new Thread(()->{
            long b = 0;
            for (int i = 0; i < 10000_0000; i++) {
                ++b;
            }
        });

        long beg = System.currentTimeMillis();
        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("fun2()的时间差为:" + (end-beg) + "ms");
    }
}
